Sorting question

Hi,

I have the following information stored in a string array:

Tuesday, October 16, 2012
Monday, October 15, 2012
Thursday, October 25, 2012
Tuesday, October 23, 2012
Monday, October 29, 2012
Friday, October 19, 2012
Monday, October 22, 2012
Wednesday, October 24, 2012
Wednesday, October 17, 2012
Thursday, October 18, 2012
Friday, October 26, 2012

Is there a way to sort these based on dates?

I wish to have the above array sorted to:

Monday, October 15, 2012
Tuesday, October 16, 2012
Wednesday, October 17, 2012
Thursday, October 18, 2012
Friday, October 19, 2012
Monday, October 22, 2012
Tuesday, October 23, 2012
Wednesday, October 24, 2012
Thursday, October 25, 2012
Friday, October 26, 2012

I see a perm to sort a module, but nothing directly related to string arrays.

Thank you
Gedinfo - Wed Oct 31 09:59:39 EDT 2012

Re: Sorting question
adevicq - Wed Oct 31 10:17:56 EDT 2012

Hi,
I would remove the day name and then do for each date as follows
 

string szDate = "October 16, 2012"
Date d = szDate
string s = stringOf(d, "yyyy/M/dd")
print s

 


These string dates are sortable (in a string skip list).

 

 

 

Skip s = createString()
 
for each row of your array do {
  Date d = aDate[i]
  put(s, stringOf(d, aDate[i]), aDate[i])
}



Then you (normally) get a sorted list when reading the content of the skip list...
I hope this can help,
Alain



 

 

Re: Sorting question
adevicq - Wed Oct 31 10:22:26 EDT 2012

adevicq - Wed Oct 31 10:17:56 EDT 2012

Hi,
I would remove the day name and then do for each date as follows
 

string szDate = "October 16, 2012"
Date d = szDate
string s = stringOf(d, "yyyy/M/dd")
print s

 


These string dates are sortable (in a string skip list).

 

 

 

Skip s = createString()
 
for each row of your array do {
  Date d = aDate[i]
  put(s, stringOf(d, aDate[i]), aDate[i])
}



Then you (normally) get a sorted list when reading the content of the skip list...
I hope this can help,
Alain



 

 

Hi again,

a more comprehensive test:
 

string aDates[] = {
"October 25, 2012",
"October 16, 2012",
"October 15, 2012",
"October 23, 2012"
}
Skip s = createString
string sz 
 
for (i = 0; i < sizeof(aDates); ++i) {
    Date d = aDates[i]
        sz = stringOf(d, "yyyy/M/dd")
        put(s, sz, aDates[i])
}
 
for sz in s do print sz "\n"
 
delete s

 


regards,
alain

 

Re: Sorting question
Gedinfo - Wed Oct 31 10:22:32 EDT 2012

The seed date is not always October 16, 2012.

This script will be run on many modules with different dates.

Is there a better method to choose the seed date?

Thank you.

Re: Sorting question
llandale - Wed Oct 31 12:24:16 EDT 2012

adevicq - Wed Oct 31 10:22:26 EDT 2012

Hi again,

a more comprehensive test:
 

string aDates[] = {
"October 25, 2012",
"October 16, 2012",
"October 15, 2012",
"October 23, 2012"
}
Skip s = createString
string sz 
 
for (i = 0; i < sizeof(aDates); ++i) {
    Date d = aDates[i]
        sz = stringOf(d, "yyyy/M/dd")
        put(s, sz, aDates[i])
}
 
for sz in s do print sz "\n"
 
delete s

 


regards,
alain

 

This is a better approach then the one I was about to suggest. However some tweaks:

string aDates[] = {
"Tuesday, October 16, 2012",
"Monday, October 15, 2012",
"Thursday, October 25, 2012",
"Tuesday, October 23, 2012",
"Monday, October 29, 2012",
"Friday, October 19, 2012",
"Monday, October 22, 2012",
"Wednesday, October 24, 2012",
"Wednesday, October 17, 2012",
"Thursday, October 18, 2012",
"Friday, October 26, 2012"
}
Skip s = create()  // KEY: 'int' date in seconds; DATA: 'string' date
string sz 
 
for (i = 0; i < sizeof(aDates); i++) {
    put(s, intOf(dateOf(aDates[i])), aDates[i])
}
i = 0 
for sz in s do
{  aDates[i++] = sz
}
for (i = 0; i < sizeof(aDates); i++) {print aDates[i] "\n"}
 
delete s


I think you could just put the "Date" as the skip KEY instead of the intOf(Date).

-Louie

Re: Sorting question
llandale - Wed Oct 31 12:29:35 EDT 2012

llandale - Wed Oct 31 12:24:16 EDT 2012

This is a better approach then the one I was about to suggest. However some tweaks:

string aDates[] = {
"Tuesday, October 16, 2012",
"Monday, October 15, 2012",
"Thursday, October 25, 2012",
"Tuesday, October 23, 2012",
"Monday, October 29, 2012",
"Friday, October 19, 2012",
"Monday, October 22, 2012",
"Wednesday, October 24, 2012",
"Wednesday, October 17, 2012",
"Thursday, October 18, 2012",
"Friday, October 26, 2012"
}
Skip s = create()  // KEY: 'int' date in seconds; DATA: 'string' date
string sz 
 
for (i = 0; i < sizeof(aDates); i++) {
    put(s, intOf(dateOf(aDates[i])), aDates[i])
}
i = 0 
for sz in s do
{  aDates[i++] = sz
}
for (i = 0; i < sizeof(aDates); i++) {print aDates[i] "\n"}
 
delete s


I think you could just put the "Date" as the skip KEY instead of the intOf(Date).

-Louie

.....
  • add "int i"
  • change "dateOf" to "getDateOf" or "date"

Re: Sorting question
adevicq - Wed Oct 31 12:39:56 EDT 2012

llandale - Wed Oct 31 12:29:35 EDT 2012
.....

  • add "int i"
  • change "dateOf" to "getDateOf" or "date"

Hi Louie,
It seems that regular skip lists are not sorted. This is why I used a string skik list. When I execute you code I get the fllowing result (DOORE 9.3):
 

Tuesday, October 16, 2012
Monday, October 15, 2012
Thursday, October 25, 2012
Tuesday, October 23, 2012
Monday, October 29, 2012
Friday, October 19, 2012
Monday, October 22, 2012
Wednesday, October 24, 2012
Wednesday, October 17, 2012
Thursday, October 18, 2012
Friday, October 26, 2012

 


alain

 

Re: Sorting question
Mathias Mamsch - Wed Oct 31 16:16:47 EDT 2012

adevicq - Wed Oct 31 12:39:56 EDT 2012

Hi Louie,
It seems that regular skip lists are not sorted. This is why I used a string skik list. When I execute you code I get the fllowing result (DOORE 9.3):
 

Tuesday, October 16, 2012
Monday, October 15, 2012
Thursday, October 25, 2012
Tuesday, October 23, 2012
Monday, October 29, 2012
Friday, October 19, 2012
Monday, October 22, 2012
Wednesday, October 24, 2012
Wednesday, October 17, 2012
Thursday, October 18, 2012
Friday, October 26, 2012

 


alain

 

Of course Skips are sorted ... If you would have inserted a print statement in the loop to fill the skip list, then you would have seen, that the intOf (...) always returns -1, therefore only putting one object to the skip, therefore overwriting the first object in the array with itself - so seemingly doing nothing.

When dealing with dates / string conversions you ALWAYS need to know the locale:
 

// Search an english locale to read the english dates ...
Locale l 
for l in supportedLocales do { 
    if  (region l == "United Kingdom") break
}
if (region l != "United Kingdom") error "Locale not found!"
 
 
// ...
 
string sz 
int  i
 
for (i = 0; i < sizeof(aDates); i++) {
        // explicitly specify the locale AND the date format here, otherwise your script will not work always
        put(s, intOf(date(aDates[i], l, "dddd, MMMM dd, yyyy") ), aDates[i])
}
 
i = 0 
for sz in s do
{  aDates[i++] = sz
}
for (i = 0; i < sizeof(aDates); i++) {print aDates[i] "\n"}
 
delete s

 


Maybe that helps, regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: Sorting question
llandale - Wed Oct 31 16:51:58 EDT 2012

adevicq - Wed Oct 31 12:39:56 EDT 2012

Hi Louie,
It seems that regular skip lists are not sorted. This is why I used a string skik list. When I execute you code I get the fllowing result (DOORE 9.3):
 

Tuesday, October 16, 2012
Monday, October 15, 2012
Thursday, October 25, 2012
Tuesday, October 23, 2012
Monday, October 29, 2012
Friday, October 19, 2012
Monday, October 22, 2012
Wednesday, October 24, 2012
Wednesday, October 17, 2012
Thursday, October 18, 2012
Friday, October 26, 2012

 


alain

 

I get good results running the code; v9.3.0.6. You did add "int i" yes and fix dateOf to date? I wonder if there is a "Locale" problem?
Results:

Monday, October 15, 2012
Tuesday, October 16, 2012
Wednesday, October 17, 2012
Thursday, October 18, 2012
Friday, October 19, 2012
Monday, October 22, 2012
Tuesday, October 23, 2012
Wednesday, October 24, 2012
Thursday, October 25, 2012
Friday, October 26, 2012
Monday, October 29, 2012


Code:

 

string aDates[] = {
    "Tuesday, October 16, 2012",
        "Monday, October 15, 2012",
        "Thursday, October 25, 2012",
        "Tuesday, October 23, 2012",
        "Monday, October 29, 2012",
        "Friday, October 19, 2012",
        "Monday, October 22, 2012",
        "Wednesday, October 24, 2012",
        "Wednesday, October 17, 2012",
        "Thursday, October 18, 2012",
        "Friday, October 26, 2012"
}
Skip s = create()  // KEY: 'int' date in seconds; DATA: 'string' date
string sz 
int i 
for (i = 0; i < sizeof(aDates); i++)
{     put(s, intOf(date(aDates[i])), aDates[i])
}
i = 0 
for sz in s do
{  aDates[i++] = sz
}
for (i = 0; i < sizeof(aDates); i++) {print aDates[i] "\n"}
 
delete s

 

Re: Sorting question
Gedinfo - Thu Nov 01 09:33:01 EDT 2012

Thanks for the help. I am running DOORS 9.4.0.0 and both suggestions work.

Re: Sorting question
adevicq - Mon Nov 05 04:46:35 EST 2012

Mathias Mamsch - Wed Oct 31 16:16:47 EDT 2012

Of course Skips are sorted ... If you would have inserted a print statement in the loop to fill the skip list, then you would have seen, that the intOf (...) always returns -1, therefore only putting one object to the skip, therefore overwriting the first object in the array with itself - so seemingly doing nothing.

When dealing with dates / string conversions you ALWAYS need to know the locale:
 

// Search an english locale to read the english dates ...
Locale l 
for l in supportedLocales do { 
    if  (region l == "United Kingdom") break
}
if (region l != "United Kingdom") error "Locale not found!"
 
 
// ...
 
string sz 
int  i
 
for (i = 0; i < sizeof(aDates); i++) {
        // explicitly specify the locale AND the date format here, otherwise your script will not work always
        put(s, intOf(date(aDates[i], l, "dddd, MMMM dd, yyyy") ), aDates[i])
}
 
i = 0 
for sz in s do
{  aDates[i++] = sz
}
for (i = 0; i < sizeof(aDates); i++) {print aDates[i] "\n"}
 
delete s

 


Maybe that helps, regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Mathias,
Thanks for the comments...
You're right: I did not check whether the return value was OK or not.
Alain